// PersistentSaveRead.cpp: implementation of the PersistentSaveRead class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "PersistentSaveDelete.h"
#include "Volume.h"
#include "AssocCommon.h"
#include "Part.h"
#include "SupportHanger.h"
#include "Joint.h"
#include "Project.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

namespace PersistentObjectOperation
{
	PersistentSaveDelete::PersistentSaveDelete()
	{
		
	}
	
	PersistentSaveDelete::~PersistentSaveDelete()
	{
		
	}

	BOOL PersistentSaveDelete::Visit(Volume*& pVolume)
	{
		ASSERT(pVolume);
		ASSERT(pVolume->IsSaveDeleteState());

		CString strSql;
		strSql.Format(_T("Delete * from [volume] where volumeID = %d"), pVolume->VolumeID());

		try
		{
			const _ConnectionPtr& pCon = this->ProjectConnection(pVolume->project());
			pCon->Execute(_bstr_t(strSql), NULL, adCmdText);
		}
		catch(_com_error e)
		{
			ASSERT(FALSE);
			ErrorAlert(e);
			return FALSE;
		}
		pVolume->SetCreateDelete();
		return TRUE;
	}

	BOOL PersistentSaveDelete::Visit(Part*& pPart)
	{
		ASSERT(pPart);
		ASSERT(pPart->IsSaveDeleteState());
		ASSERT(pPart->SupportHangerOwner());

		CString strSql;
		strSql.Format(_T("delete * from [Parts_Of_SupportHanger] where SH_ID = '%s'	and Part_ID = %d"),
				pPart->SupportHangerOwner()->SupportHangerID(),
				pPart->PartIndex());

		this->PersistentExecute(strSql, pPart->project());
		
		strSql.Format(_T("delete * from [Properties_Of_Part] where SH_ID = '%s' and Part_ID = %d"),
				pPart->SupportHangerOwner()->SupportHangerID(),
				pPart->PartIndex());

		pPart->SetCreateDelete();
		return this->PersistentExecute(strSql, pPart->project());
	}

	BOOL PersistentSaveDelete::Visit(SupportHanger*& pSH)
	{
		ASSERT(pSH);
		ASSERT(pSH->IsSaveDeleteState());


		CString strSql;

		strSql.Format(_T("delete * from [Properties_Of_Part] where SH_ID = '%s' and Part_ID = %d"),
				pSH->SupportHangerID(),
				(INT)pSH->Id());

		this->PersistentExecute(strSql, pSH->project());

		strSql.Format(_T("delete * from [SupportHanger] where SH_ID = '%s' and SH_VolumeID = '%s'"),
			pSH->SupportHangerID(),
			pSH->GetSupportHangerPoint().VolumeID());
		
		pSH->SetCreateDelete();
		return this->PersistentExecute(strSql, pSH->project());
	}

	BOOL PersistentSaveDelete::Visit(Joint*& pJoint)
	{
		ASSERT(pJoint);
		ASSERT(pJoint->IsSaveDeleteState());
		ASSERT(pJoint->SupportHangerOwner());

		CString strSql;
		strSql.Format(_T("delete * from [Joints_Of_SupportHanger] where SH_ID = '%s' and Joint_ID = %d"),
			pJoint->SupportHangerOwner()->SupportHangerID(),
			pJoint->JointIndex());
		this->PersistentExecute(strSql, pJoint->project());

		strSql.Format(_T("delete * from [Properties_Of_Part] where SH_ID = '%s' and Part_ID = %d"),
			pJoint->SupportHangerOwner()->SupportHangerID(),
			pJoint->JointIndex());

		Joint::Iterator* pJit = pJoint->NewAttachIterator();
		for(pJit->Begin(); !pJit->End(); pJit->Next())
		{
			Part* pPart = pJit->Get();
			ASSERT(pPart);
			ASSERT(pPart->project());
			pPart->project()->versionController()
				->CurPersistentVisitor().Visit(pPart);
		}
		pJoint->SetCreateDelete();
		return this->PersistentExecute(strSql, pJoint->project()); 
	}

}